package com.hk.utils;

import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.commons.dbcp2.BasicDataSourceFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 *加载配置文件，然后返回DataSource数据源
 * @author Administrator
 * */
public class PropertiesUtils {
    private static Properties properties=new Properties();
    static{
        InputStream inputStream=PropertiesUtils.class.getClassLoader().getResourceAsStream("db.properties");
        try {
            properties.load(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

//  获取数据
    public static DataSource getDataSource() throws Exception {
        try {
            return BasicDataSourceFactory.createDataSource(properties);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
